<?php
	####################################################
	# Mover 페이지 리디렉션 & 팝업 제어 컨트롤         #
	####################################################

	class Mover {
		private static $home = INDEX_FILE;
		private static $admin_home = ADMIN_INDEX_FILE;
		private static $page_error = PAGE_ERROR;
		private static $meta = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';


		# 홈 주소를 얻는다.
		public static function get_index(){
			return Mover::$home;
		}

		# 기본적인 리디렉션
		public static function redirect($redirect_url, $is_exit = FALSE) {
			echo Mover::$meta;
			echo "<meta http-equiv='Refresh' content='0; URL=".$redirect_url."'>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 메시지를 보여주면서 리디렉션 시킨다.
		#	example)
		#	Mover::redirect_with_msg(Mover::get_index(), "goto home.php?mode=1", TRUE);
		public static function redirect_msg($redirect_url, $message, $is_exit = FALSE) {
			echo Mover::$meta;
			echo "<script>alert(\"".$message."\");</script>";
			echo "<meta http-equiv='Refresh' content='0; URL=".$redirect_url."'>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 리디렉션 시키기전 스크립트를 실행 시키고 리디렉션 한다.
		public static function redirect_script($redirect_url, $inline_script, $is_exit = FALSE) {
			echo Mover::$meta;
			echo "<script>".$inline_script."</script>";
			echo "<meta http-equiv='Refresh' content='0; URL=".$redirect_url."'>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 홈으로 이동한다.
		public static function home($message = '', $is_exit = FALSE) {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}
			echo "<meta http-equiv='Refresh' content='0; URL=".Mover::$home."'>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 어드민 홈으로 이동한다.
		public static function admin_home($message = '', $is_exit = FALSE) {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}
			echo "<meta http-equiv='Refresh' content='0; URL=".Mover::$admin_home."'>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 뒤로 이동한다.
		public static function back($message = '', $is_exit = FALSE) {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}
			echo "<script>history.go(-1);</script>";
			if($is_exit == TRUE){
				exit();
			}
		}

		# 유저가 권한이 없을 경우 홈으로 이동시키는 형태.
		# 404 혹은 기타 다른 페이지로 이동 시킬 수 있으므로... Mover::home과 분리한다.
		public static function cannot_access($message = '') {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}
			echo "<meta http-equiv='Refresh' content='0; URL=".Mover::$page_error."'>";
			exit();
		}

		# 위 함수와 역할이 같다.
		public static function error($message = '') {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}
			echo "<meta http-equiv='Refresh' content='0; URL=".Mover::$page_error."'>";
			exit();
		}

		# 현재 페이지가 창(popup, window, modal)인 경우 닫는 처리
		public static function close_child($message = '', $parent_reload = FALSE, $parent_replace = '', $is_exit = FALSE, $script_code = '') {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}

			if($parent_reload == TRUE){
				echo "<script>opener.parent.location.reload();</script>";
			}

			if($parent_replace != ''){
				echo "<script>opener.parent.location.replace(\"".$parent_replace."\");</script>";
			}

			if($script_code != ''){
				echo "<script>";
				echo $script_code;
				echo "</script>";
			}

			echo "<script>";
			echo "window.open('','_self').close();";
			echo "window.close();";
			echo "</script>";

			if($is_exit == TRUE){
				exit();
			}
		}

		# 부모창을 새로고침.
		public static function parent_reload($message = '', $parent_reload = TRUE, $parent_replace = '') {
			echo Mover::$meta;
			if($message != ''){
				echo "<script>alert(\"".$message."\");</script>";
			}

			if($parent_reload == TRUE){
				echo "<script>parent.location.reload();</script>";
			}

			if($parent_replace != ''){
				echo "<script>parent.location.replace(\"".$parent_replace."\");</script>";
			}
		}

		# POST 페이지 이동
		public static function redirect_post($url, $element, $target='') {
			if ($target) $target .= ".";
			echo "
				<script>
				ce = {$target}document.createElement('form');
				ce.setAttribute('method', 'post');
				ce.setAttribute('action', '{$url}');
				var fm = {$target}document.getElementById('jsmotion').appendChild(ce);
			";
			if (is_array($element)){
				foreach($element as $k => $v){
					echo "
						ce = {$target}document.createElement('input');
						ce.setAttribute('type', 'hidden');
						ce.setAttribute('name', '{$k}');
						ce.setAttribute('value', '{$v}');
						fm.appendChild(ce);
					";
				}
			}
			echo "
				fm.submit();
				history.back();
				</script>
			";
			exit;
		}
	}

?>